package com.example.day11.homework;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreeThread {
    //通信对象 0---打印大写字母----1打印数字-----2打印小写字母
    private  int number=0;
    //声明锁
    private Lock lock=new ReentrantLock();
    //声明钥匙
    private Condition conditionA=lock.newCondition();
    private Condition conditionB=lock.newCondition();
    private Condition conditionC=lock.newCondition();
    //打印大写字母
    public  void  printcapitalized(){
            try {
                lock.lock();
                for(char i='A';i<='Z';i++){

                    while (number!=0){
                        conditionA.await();
                    }
                    System.out.print(i);
                    number=1;
                    conditionB.signal();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                lock.unlock();
            }

    }
    //打印数字
    public   void   printCount(){
        try {
            lock.lock();
            for(int i=1;i<=26;i++){

                while (number!=1){
                    conditionB.await();
                }
                System.out.print(i);
                number=2;
                conditionC.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }
    //打印小写字母
    public  void   printLoweer(){
        try {
            lock.lock();
            for(char i='a';i<='z';i++){

                while (number!=2){
                    conditionC.await();
                }
                System.out.print(i);
                number=0;
                conditionA.signal();

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
        }
    }

}

class   TestPint{
    public static void main(String[] args) {
        ThreeThread three=new ThreeThread();
        new Thread(()->{
            three.printcapitalized();   //打印大写字母
        }).start();
        new Thread(()->{
            three.printCount();      //打印数字
        }).start();
        new Thread(()->{
            three.printLoweer();   //打印小写字母
        }).start();
    }
}
